# encoding: utf-8
require File.expand_path('../models/PageModel', File.dirname(__FILE__))
#require File.expand_path('../core/Controller', File.dirname(__FILE__))
require 'json'

class Page < Controller
  def addForm
    # http://192.168.1.111:4567/do/Page/addForm
    @view.showHtml("pageAdd", @path, nil)
  end

  def add
    p=PageModel.new
    p.title=@params[:title]
    p.gcontents=@params[:gcontent]
    if p.save
      #  city=Array.new
      #  city.append "guava"
      #  city.append "session"
      # city.to_json
      p.to_json
    else
      fie="fail"
      fie.to_json
    end

  end

  def all
    ps=PageModel.all
    shtml="<a href=http://www.19mm.com:4567/do/Page/addForm  target=_blank>添加文章</a><br>"
    for i in ps do
      # shtml+=i.title+"--<a href=http://www.19mm.com:4567/do/Page/del?id=#{i.id}>删除</a><br>"
      shtml+=i.title+" id:#{i.id}--<a href=http://www.19mm.com:4567/do/Page/editForm?id=#{i.id}>编辑</a> <a href=http://www.19mm.com:4567/do/Page/del?id=#{i.id}>删除</a>"+"--<a href=http://www.19mm.com:4567/do/Page/get?id=#{i.id}&callback=cc>json获取接口</a><br>"
    end
    shtml
  end


  def del
    # http://192.168.1.111:4567/do/Page/del?id=3
    page=PageModel.find_by(id: @params[:id])
    if page.destroy
      fie="删除成功"
      fie.to_json
    end
  end

  def get
    # http://192.168.1.111:4567/do/Page/del?id=3
    callback=@params[:callback]
    page=PageModel.find_by(id: @params[:id])
    # page.to_json


    callback+"(["+page.to_json+"])"
  end


  def editForm
    # http://192.168.1.111:4567/do/Page/del?id=3
    page=PageModel.find_by(id: @params[:id])
    puts page.title
    page.gcontents=page.gcontents.force_encoding("UTF-8")
    # page.title="guava"
    @view.showHtml("pageEdit", @path, page)
  end

  def edit
    p=PageModel.find_by(id: @params[:id])
    p.title=@params[:title]
    p.gcontents=@params[:gcontent]
    if p.save
      #  city=Array.new
      #  city.append "guava"
      #  city.append "session"
      # city.to_json
      p.to_json
    else
      fie="fail"
      fie.to_json
    end

  end

end

